﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using static HaumDisplay.HaumDisplayCallBack;

namespace HaumDisplay
{
    /// <summary>
    /// HaumDisplayWindow图像显示控件
    /// </summary>
    public partial class HaumDisplayWindow : UserControl
    {

        #region 回调
        /// <summary>
        /// 导入图片回调
        /// </summary>
        public ImportImageCallBack importImageCallBack;
        /// <summary>
        /// 加载图片回调
        /// </summary>
        public LoadImageCallBack loadImageCallBack;
        /// <summary>
        /// 显示图片回调
        /// </summary>
        public ShowImageCallBack showImageCallBack;
        /// <summary>
        /// 移除图片回调
        /// </summary>
        public RemoveImageCallBack removeImageCallBack;
        /// <summary>
        /// 绘制点回调
        /// </summary>
        public GraphicPointCallBack graphicPointCallBack;
        /// <summary>
        /// 绘制线回调
        /// </summary>
        public GraphicLineCallBack graphicLineCallBack;
        /// <summary>
        /// 绘制圆回调
        /// </summary>
        public GraphicCircleCallBack graphicCircleCallBack;
        /// <summary>
        /// 绘制椭圆回调
        /// </summary>
        public GraphicElipseCallBack graphicElipseCallBack;
        /// <summary>
        /// 绘制矩形回调
        /// </summary>
        public GraphicRectangleCallBack graphicRectangleCallBack;
        /// <summary>
        /// 绘制旋转矩形回调
        /// </summary>
        public GraphicRotateRectCallBack graphicRotateRectCallBack;
        /// <summary>
        /// 绘制多边形回调
        /// </summary>
        public GraphicPolygonCallBack graphicPolygonCallBack;

        #endregion

        #region 图像基础操作
        /// <summary>
        /// 显示图像
        /// </summary>
        public HaumImage hImage;
        /// <summary>
        /// 缩略图
        /// </summary>
        private Dictionary<int, Bitmap> thumbnailDic = new Dictionary<int, Bitmap>();
        /// <summary>
        /// 图像锁
        /// </summary>
        private object bmpLock = new object();
        /// <summary>
        /// 开始移动时鼠标位置
        /// </summary>
        private Point startMovePosition = Point.Empty;
        /// <summary>
        /// 图像显示模式 ： 高质量（平滑处理锯齿不失真）， 像素级（能够清晰显示每一个像素，锯齿边缘明显，适合查看像素坐标与rgb值）
        /// </summary>
        private HAUM_DISPALY_MODE viewMode = HAUM_DISPALY_MODE.HighPerformance;

        #endregion

        #region 图形绘制操作
        /// <summary>
        /// 绘图对象
        /// </summary>
        private GraphicHelper myGraphic = new GraphicHelper();
        /// <summary>
        /// 绘制点对象
        /// </summary>
        private HaumPoint haumPoint;
        /// <summary>
        /// 绘制线对象
        /// </summary>
        private HaumLine haumLine;
        /// <summary>
        /// 绘制圆对象
        /// </summary>
        private HaumCircle haumCircle;
        /// <summary>
        /// 绘制椭圆
        /// </summary>
        private HaumEllipse haumEllipse;
        /// <summary>
        /// 绘制矩形对象
        /// </summary>
        private HaumRectangle haumRectangle;
        /// <summary>
        /// 绘制可旋转矩形
        /// </summary>
        private HaumRotateRect haumRotateRect;
        /// <summary>
        /// 绘制多边形对象
        /// </summary>
        private HaumPolygon haumPolygon;
        /// <summary>
        /// 文本
        /// </summary>
        private HaumText haumText;
        /// <summary>
        /// 显示图形集合
        /// </summary>
        private List<HaumShape> shapes = new List<HaumShape>();
        #endregion

        #region 像素与坐标信息
        /// <summary>
        /// 显示像素信息控件
        /// </summary>
        private HaumPixelInfo pixelInfo = new HaumPixelInfo();
        /// <summary>
        /// 是否正在显示图片
        /// </summary>
        public bool isDisplaying;
        /// <summary>
        /// 是否位于mainView内
        /// </summary>
        private bool isMouseInMainView;
        /// <summary>
        /// 是否位于显示的图片内
        /// </summary>
        private bool isMouseInImage;
        /// <summary>
        /// 当前鼠标相对于mainView的坐标点
        /// </summary>
        private PointF mouseLocation = PointF.Empty;
        /// <summary>
        /// 鼠标在相对图片坐标
        /// </summary>
        private PointF imageLocation = PointF.Empty;
        /// <summary>
        /// 鼠标
        /// </summary>
        private Cursor cursor;
        /// <summary>
        /// 监听快捷键线程
        /// </summary>
        private Thread listenKeys;
        #endregion

        #region 图像属性信息
        private ContextMenu menu;
        /// <summary>
        /// 信息窗口
        /// </summary>
        private HaumImageInfo imageInfo;
        #endregion

        #region 控件风格
        [Category("WindowStyle")]//(属性类)
        [Description("导航栏背景颜色")]// 
        public Color BarBackColor
        {
            get { return toolStrip_menu.BackColor; }
            set { toolStrip_menu.BackColor = value; }
        }
        [Category("WindowStyle")]//(属性类)
        [Description("导航栏前景颜色")]// 
        public Color BarForeColor
        {
            get { return toolStrip_menu.ForeColor; }
            set { toolStrip_menu.ForeColor = value; }
        }
        [Category("WindowStyle")]//(属性类)
        [Description("显示窗口颜色")]// 
        public Color WindowColor
        {
            get { return mainView.BackColor; }
            set { mainView.BackColor = value; }
        }
        #endregion

        /// <summary>
        /// 初始化控件
        /// </summary>
        public HaumDisplayWindow()
        {
            InitializeComponent();
            InitContextMenu();
            //绑定显示控件鼠标滑动事件 进行缩放
            mainView.MouseWheel += mainView_MouseWheel;
            // 启用控件双缓冲 防止控件重绘闪烁
            this.DoubleBuffered = true;
            this.cursor = Cursors.Arrow;
        }

        /// <summary>
        /// 监听快捷键
        /// </summary>
        private void ListenKeys()
        {
            while (this.Created)
            {
                if (Control.ModifierKeys == Keys.Control && isDisplaying && isMouseInMainView && isMouseInImage)
                {
                    Point xy = new Point((int)imageLocation.X, (int)imageLocation.Y);
                    lock (hImage.bmp)
                    {
                        Color rgb = hImage.bmp.GetPixel(xy.X, xy.Y);
                        //显示提示
                        this.BeginInvoke(new Action(delegate
                        {
                            if (pixelInfo.Visible == false)
                            {
                                pixelInfo.Visible = true;
                            }
                            pixelInfo.SetPixelInfo(xy, rgb, new Point(Control.MousePosition.X + 10, Control.MousePosition.Y + 15));
                        }));
                    }

                }
                else
                {
                    this.BeginInvoke(new Action(delegate
                    {
                        if (pixelInfo.Visible == true)
                        {
                            pixelInfo.Visible = false;
                        }
                    }));
                }
                Thread.Sleep(10);
            }
        }

        /// <summary>
        /// 显示图片
        /// </summary>
        /// <param name="image">图片</param>
        public void ShowImage(Image image, bool isFit = true, string path = "")
        {
            try
            {
                mainView.Cursor = Cursors.WaitCursor;
                this.ClearDraw();
                this.RemoveImage();
                this.hImage = new HaumImage(image, path);
                this.isDisplaying = true;
                this.DisplayStrategy();
                if (isFit) this.Fit();
                if (myGraphic.showThums) this.thumbnailDic = Tools.CreateThumbnails(this.hImage.bmp, myGraphic.ratio);
                this.showImageCallBack?.Invoke(this.hImage);
                mainView.Cursor = this.cursor;
            }
            catch (Exception ex)
            {
                throw new ArgumentException("显示图片异常~");
            }

        }

        /// <summary>
        /// 显示策略
        /// </summary>
        private void DisplayStrategy()
        {
            if (this.hImage.size.Width * this.hImage.size.Height > 12000000)
            {
                myGraphic.showThums = true;
            }
            else
            {
                myGraphic.showThums = false;
            }
        }

        /// <summary>
        /// 图像自适应
        /// </summary>
        public void Fit()
        {
            if (this.isDisplaying)
            {
                //计算控件与图像的宽高比例
                float scaleX = (float)(mainView.Width) / hImage.bmp.Width;
                float scaleY = (float)(mainView.Height) / hImage.bmp.Height;
                //获取较小的一边比例作为缩放比例
                myGraphic.scale = Math.Min(scaleX, scaleY);
                //获取原图与控件初始比例
                myGraphic.ratio = myGraphic.scale;

                //计算缩放后的图像宽高值
                myGraphic.scaleSize.Width = (int)(hImage.bmp.Width * myGraphic.scale);
                myGraphic.scaleSize.Height = (int)(hImage.bmp.Height * myGraphic.scale);

                //计算图像居中显示的位置
                myGraphic.offset.X = (mainView.Width - myGraphic.scaleSize.Width) / 2;
                myGraphic.offset.Y = (mainView.Height - myGraphic.scaleSize.Height) / 2;

                toolStripSplitButton_scale.Text = $"{(myGraphic.scale * 100).ToString("0")}%";

                //触发自身的Point方法进行控件重绘
                mainView.Invalidate();
            }
        }

        /// <summary>
        /// 导入图片
        /// </summary>
        public void ImportImage(bool fit = true)
        {
            //打开文件对话框进行选择图片并触发显示控件重绘
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "图像文件(*.bmp;*.png;*.jpg;*.jpeg;*.tif;*.tiff;*.gif)|*.bmp;*.png;*.jpg;*.jpeg;*.tif;*.tiff;*.gif|所有文件(*.*)|*.*";
                ofd.Title = "选择导入并显示图片";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        this.LoadImage(ofd.FileName, fit);
                        this.importImageCallBack?.Invoke(this.hImage);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        /// <summary>
        /// 根据路径加载图片
        /// </summary>
        /// <param name="path">图片文件路径</param>
        public void LoadImage(string path, bool fit)
        {
            if (File.Exists(path))
            {
                try
                {
                    using (Image img = Image.FromFile(path))
                    {
                        this.ShowImage(img, fit, path);
                    }
                    this.loadImageCallBack?.Invoke(this.hImage);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("加载失败，可能的原因是：文件已损坏。");
                }
            }
        }

        /// <summary>
        /// 移除图片
        /// </summary>
        public void RemoveImage()
        {
            if (isDisplaying)
            {
                this.ClearDraw();
                this.hImage?.Dispose();
                this.hImage = null;
                this.thumbnailDic.Clear();
                this.isDisplaying = false;
                this.mainView.Invalidate();
                this.removeImageCallBack?.Invoke();
            }
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        public bool SaveImageDialog()
        {
            if (this.isDisplaying)
            {
                //打开保存文件对话框保存图片
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Title = "保存图片到：";
                    sfd.DefaultExt = "bmp";
                    sfd.CheckPathExists = true;
                    sfd.Filter = "BMP|*.bmp|PNG|*.png|JPG|*.jpg|JPEG|*.jpeg|TIF|*.tif|TIFF|*.tiff|GIF|*.gif";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        return this.SaveImage(sfd.FileName);
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        public bool SaveImage(string path)
        {
            if (this.isDisplaying)
            {
                try
                {

                    ImageFormat format = Tools.GetImageFormat(path);
                    hImage.bmp.Save(path, format);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return false;
        }

        /// <summary>
        /// 保存控件上的绘制内容为图片
        /// </summary>
        /// <param name="path">保存路径（支持bmp、png、jpg等格式）</param>
        /// <returns>是否保存成功</returns>
        public bool SaveGraphics(string path)
        {
            if (this.isDisplaying)
            {
                try
                {
                    // 创建与控件大小相同的位图
                    using (Bitmap bmp = new Bitmap(mainView.Width, mainView.Height))
                    {
                        // 将控件内容绘制到位图上
                        mainView.DrawToBitmap(bmp, mainView.ClientRectangle);
                        // 保存图片
                        ImageFormat format = Tools.GetImageFormat(path);
                        bmp.Save(path, format);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return false;
        }

        /// <summary>
        /// 保存控件上的绘制内容为图片
        /// </summary>
        /// <param name="path">保存路径（支持bmp、png、jpg等格式）</param>
        /// <returns>是否保存成功</returns>
        public bool SaveGraphicsDialog()
        {
            if (this.isDisplaying)
            {
                //打开保存文件对话框保存图片
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Title = "保存图片到：";
                    sfd.DefaultExt = "bmp";
                    sfd.CheckPathExists = true;
                    sfd.Filter = "BMP|*.bmp|PNG|*.png|JPG|*.jpg|JPEG|*.jpeg|TIF|*.tif|TIFF|*.tiff|GIF|*.gif";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        return this.SaveGraphics(sfd.FileName);
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="path">文件名（带后缀）</param>
        /// <param name="quality">压缩质量：当文件后缀名为jpg或jpeg时自动启用 取值范围(最差)0-100(最优)</param>
        public bool SaveJpegQuality(string path, byte quality = 100)
        {
            ImageFormat format = Tools.GetImageFormat(path);
            //如果图像未加载或文件后缀不匹配直接返回
            if (this.isDisplaying && format == ImageFormat.Jpeg)
            {

                try
                {
                    // 获取 JPEG 编码器
                    ImageCodecInfo jpegCodecInfo = Tools.GetEncoderInfo("image/jpeg");
                    // 创建 EncoderParameters 对象，设置压缩质量
                    EncoderParameters encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                    //保存
                    hImage.bmp.Save(path, jpegCodecInfo, encoderParameters);

                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return false;
        }

        /// <summary>
        /// 鼠标在显示区域移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainView_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDisplaying)
            {
                //实时计算拖动图片后的鼠标位置并重绘
                if (this.cursor == Cursors.Hand && e.Button == MouseButtons.Left && myGraphic.type == HAUM_GRAPHIC_TYPE.None)
                {
                    int dx = e.X - startMovePosition.X;
                    int dy = e.Y - startMovePosition.Y;
                    if (Math.Abs(dx) > 2 || Math.Abs(dy) > 2)
                    {
                        myGraphic.offset.X += dx;
                        myGraphic.offset.Y += dy;
                        startMovePosition.X = e.Location.X;
                        startMovePosition.Y = e.Location.Y;
                        mainView.Invalidate();
                    }
                }

                //记录鼠标基于mainView位置
                mouseLocation = e.Location;
                //记录鼠标基于当前图片位置
                imageLocation = Tools.ScreenTransImagePointF(e.Location, myGraphic.scale, myGraphic.offset);
                isMouseInImage = Tools.IsMouseInImage(imageLocation, this.hImage.size);

                switch (myGraphic.type)
                {
                    case HAUM_GRAPHIC_TYPE.Point:
                        DrawPoint(e);
                        mainView.Invalidate();
                        break;
                    case HAUM_GRAPHIC_TYPE.Line:
                        DrawLine(e);
                        mainView.Invalidate();
                        break;
                    case HAUM_GRAPHIC_TYPE.Rectangle:
                        DrawRectangle(e);
                        mainView.Invalidate();
                        break;
                    case HAUM_GRAPHIC_TYPE.Circle:
                        DrawCircle(e);
                        mainView.Invalidate();
                        break;
                    case HAUM_GRAPHIC_TYPE.RotateRect:
                        DrawRotateRect(e);
                        mainView.Invalidate();
                        break;
                    case HAUM_GRAPHIC_TYPE.Polygon:
                        DrawPolygon(e, false);
                        mainView.Invalidate();
                        break;
                }
            }
        }

        /// <summary>
        /// 鼠标滚轮滚动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainView_MouseWheel(object sender, MouseEventArgs e)
        {
            if (this.isDisplaying)
            {
                float zoomFactor = e.Delta > 0 ? 1.1f : 0.9f;
                float oldScale = myGraphic.scale;
                myGraphic.scale *= zoomFactor;

                // 限制缩放范围
                if (myGraphic.scale < 0.01f) myGraphic.scale = 0.01f;
                if (myGraphic.scale > 100.0f) myGraphic.scale = 100.0f;

                // 根据鼠标位置调整偏移量
                float dx = (float)e.X - myGraphic.offset.X;
                float dy = (float)e.Y - myGraphic.offset.Y;
                myGraphic.offset.X -= dx * (myGraphic.scale / oldScale - 1);
                myGraphic.offset.Y -= dy * (myGraphic.scale / oldScale - 1);

                myGraphic.scaleSize.Width = (int)(hImage.bmp.Width * myGraphic.scale);
                myGraphic.scaleSize.Height = (int)(hImage.bmp.Height * myGraphic.scale);

                //更新缩放率
                toolStripSplitButton_scale.Text = $"{(myGraphic.scale * 100).ToString("0")}%";

                mainView.Invalidate();
            }
        }

        /// <summary>
        /// 控件重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainView_Paint(object sender, PaintEventArgs e)
        {
            if (this.isDisplaying)
            {
                // 获取控件的工作区域
                Rectangle clientArea = mainView.ClientRectangle;
                // 设置绘制区域限制
                e.Graphics.SetClip(clientArea);

                switch (viewMode)
                {
                    case HAUM_DISPALY_MODE.HighPerformance:
                        e.Graphics.SmoothingMode = SmoothingMode.None;
                        e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
                        e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
                        e.Graphics.CompositingQuality = CompositingQuality.HighSpeed;
                        e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                        break;
                    case HAUM_DISPALY_MODE.HighQuality:
                        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        e.Graphics.CompositingQuality = CompositingQuality.AssumeLinear;
                        e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                        break;
                }

                if (myGraphic.showThums)
                {
                    // 绘制缩放后的图片
                    if (myGraphic.scale / myGraphic.ratio < 10)
                    {
                        int level = (int)(myGraphic.scale / myGraphic.ratio);
                        switch (level)
                        {
                            case 0:
                                e.Graphics.DrawImage(thumbnailDic[0], myGraphic.offset.X, myGraphic.offset.Y, myGraphic.scaleSize.Width, myGraphic.scaleSize.Height);
                                break;
                            case 1:
                            case 2:
                            case 3:
                            case 4:
                                e.Graphics.DrawImage(thumbnailDic[1], myGraphic.offset.X, myGraphic.offset.Y, myGraphic.scaleSize.Width, myGraphic.scaleSize.Height);
                                break;
                            case 5:
                            case 6:
                            case 7:
                            case 8:
                            case 9:
                                e.Graphics.DrawImage(thumbnailDic[2], myGraphic.offset.X, myGraphic.offset.Y, myGraphic.scaleSize.Width, myGraphic.scaleSize.Height);
                                break;
                        }
                    }
                    else
                    {
                        lock (hImage.bmp)
                        {
                            e.Graphics.DrawImage(hImage.bmp, myGraphic.offset.X, myGraphic.offset.Y, myGraphic.scaleSize.Width, myGraphic.scaleSize.Height);
                        }
                    }
                }
                else
                {
                    lock (hImage.bmp)
                    {
                        e.Graphics.DrawImage(hImage.bmp, myGraphic.offset.X, myGraphic.offset.Y, myGraphic.scaleSize.Width, myGraphic.scaleSize.Height);
                    }
                }


                if (haumPoint != null && haumPoint.startGraphic)
                {
                    haumPoint.Graphic(e.Graphics, myGraphic);
                }

                if (haumLine != null && haumLine.startGraphic)
                {
                    haumLine.Graphic(e.Graphics, myGraphic);
                }

                if (haumCircle != null && haumCircle.startGraphic)
                {
                    haumCircle.Graphic(e.Graphics, myGraphic);
                }

                if (haumEllipse != null && haumEllipse.startGraphic)
                {

                }

                if (haumRectangle != null && haumRectangle.startGraphic)
                {
                    haumRectangle.Graphic(e.Graphics, myGraphic);
                }

                if (haumRotateRect != null && haumRotateRect.startGraphic)
                {
                    haumRotateRect.Graphic(e.Graphics, myGraphic);
                }

                if (haumPolygon != null && haumPolygon.startGraphic)
                {
                    haumPolygon.Graphic(e.Graphics, myGraphic);
                }

                if (haumText != null)
                {
                    ShowText(e);
                }

                if (shapes.Count > 0)
                {

                    for (int i = 0; i < shapes.Count; i++)
                    {
                        shapes[i].Graphic(e.Graphics, myGraphic);
                    }
                }
            }
        }



        private void InitContextMenu()
        {
            menu = new ContextMenu();
            menu.MenuItems.Add("导入图像");
            menu.MenuItems[0].Click += delegate { this.ImportImage(); };
            menu.MenuItems.Add("自适应");
            menu.MenuItems[1].Click += delegate { this.Fit(); };
            menu.MenuItems.Add("显示");
            menu.MenuItems[2].MenuItems.Add("高性能");
            menu.MenuItems[2].MenuItems[0].Click += delegate { viewMode = HAUM_DISPALY_MODE.HighPerformance; };
            menu.MenuItems[2].MenuItems.Add("高质量");
            menu.MenuItems[2].MenuItems[1].Click += delegate { viewMode = HAUM_DISPALY_MODE.HighQuality; };
            menu.MenuItems.Add("旋转");
            menu.MenuItems[3].MenuItems.Add("水平翻转");
            menu.MenuItems[3].MenuItems[0].Click += delegate { this.FlipX(); };
            menu.MenuItems[3].MenuItems.Add("垂直翻转");
            menu.MenuItems[3].MenuItems[1].Click += delegate { this.FlipY(); };
            menu.MenuItems[3].MenuItems.Add("顺时针旋转");
            menu.MenuItems[3].MenuItems[2].Click += delegate { this.RightRotate(); };
            menu.MenuItems[3].MenuItems.Add("逆时针旋转");
            menu.MenuItems[3].MenuItems[3].Click += delegate { this.LeftRotate(); };
            menu.MenuItems.Add("绘制图形");
            menu.MenuItems[4].MenuItems.Add("Point");
            menu.MenuItems[4].MenuItems[0].Click += delegate { this.StartDrawPoint(); };
            menu.MenuItems[4].MenuItems.Add("Line");
            menu.MenuItems[4].MenuItems[1].Click += delegate { this.StartDrawLine(); };
            menu.MenuItems[4].MenuItems.Add("Circle");
            menu.MenuItems[4].MenuItems[2].Click += delegate { this.StartDrawCircle(); };
            menu.MenuItems[4].MenuItems.Add("Ellipse");
            menu.MenuItems[4].MenuItems[3].Click += delegate { this.StartDrawEllipse(); };
            menu.MenuItems[4].MenuItems.Add("Rectangle");
            menu.MenuItems[4].MenuItems[4].Click += delegate { this.StartDrawRectangle(); };
            menu.MenuItems[4].MenuItems.Add("RotateRect");
            menu.MenuItems[4].MenuItems[5].Click += delegate { this.StartDrawRotateRect(); };
            menu.MenuItems[4].MenuItems.Add("Polygon");
            menu.MenuItems[4].MenuItems[6].Click += delegate { this.StartDrawPolygon(); };
            //menu.MenuItems.Add("宽");
            //menu.MenuItems[5].MenuItems.Add("1");
            //menu.MenuItems[5].MenuItems[0].Click += delegate { this.myGraphic.size = 1; };
            //menu.MenuItems[5].MenuItems.Add("2");
            //menu.MenuItems[5].MenuItems[1].Click += delegate { this.myGraphic.size = 2; };
            //menu.MenuItems[5].MenuItems.Add("3");
            //menu.MenuItems[5].MenuItems[2].Click += delegate { this.myGraphic.size = 3; };
            //menu.MenuItems[5].MenuItems.Add("4");
            //menu.MenuItems[5].MenuItems[3].Click += delegate { this.myGraphic.size = 4; };
            //menu.MenuItems[5].MenuItems.Add("5");
            //menu.MenuItems[5].MenuItems[4].Click += delegate { this.myGraphic.size = 5; };
            menu.MenuItems.Add("颜色");
            menu.MenuItems[5].MenuItems.Add("White");
            menu.MenuItems[5].MenuItems[0].Click += delegate { this.myGraphic.color = Color.White; };
            menu.MenuItems[5].MenuItems.Add("Black");
            menu.MenuItems[5].MenuItems[1].Click += delegate { this.myGraphic.color = Color.Black; };
            menu.MenuItems[5].MenuItems.Add("Red");
            menu.MenuItems[5].MenuItems[2].Click += delegate { this.myGraphic.color = Color.Red; };
            menu.MenuItems[5].MenuItems.Add("Green");
            menu.MenuItems[5].MenuItems[3].Click += delegate { this.myGraphic.color = Color.Green; };
            menu.MenuItems[5].MenuItems.Add("Blue");
            menu.MenuItems[5].MenuItems[4].Click += delegate { this.myGraphic.color = Color.Blue; };
            menu.MenuItems[5].MenuItems.Add("Yellow");
            menu.MenuItems[5].MenuItems[5].Click += delegate { this.myGraphic.color = Color.Yellow; };
            menu.MenuItems.Add("窗口");
            menu.MenuItems[6].MenuItems.Add("保存");
            menu.MenuItems[6].MenuItems[0].Click += delegate { this.SaveGraphicsDialog(); };
            menu.MenuItems[6].MenuItems.Add("清除图形");
            menu.MenuItems[6].MenuItems[1].Click += delegate { this.ClearDraw(); };
            menu.MenuItems[6].MenuItems.Add("移除图像");
            menu.MenuItems[6].MenuItems[2].Click += delegate { this.RemoveImage(); };
            menu.MenuItems.Add("保存图片");
            menu.MenuItems[7].Click += delegate { this.SaveImageDialog(); };
            menu.MenuItems.Add("属性");
            menu.MenuItems[8].Click += delegate { this.ShowProperties(); };

        }


        private void ShowProperties()
        {
            if (imageInfo == null || imageInfo.IsDisposed)
            {
                imageInfo = new HaumImageInfo(hImage, Control.MousePosition);
            }
            imageInfo.Show();
        }

        /// <summary>
        /// 绘制区域鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainView_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && mainView.Cursor == Cursors.Hand)
            {
                startMovePosition = e.Location;
            }

            if (e.Button == MouseButtons.Right && myGraphic.type == HAUM_GRAPHIC_TYPE.None && myGraphic.status == HAUM_GRAPHIC_STATUS.Done)
            {
                menu.Show(mainView, e.Location);
            }

            switch (myGraphic.type)
            {
                case HAUM_GRAPHIC_TYPE.Point:
                    DrawPoint(e);
                    break;
                case HAUM_GRAPHIC_TYPE.Line:
                    DrawLine(e);
                    break;
                case HAUM_GRAPHIC_TYPE.Rectangle:
                    DrawRectangle(e);
                    break;
                case HAUM_GRAPHIC_TYPE.RotateRect:
                    DrawRotateRect(e);
                    break;
                case HAUM_GRAPHIC_TYPE.Circle:
                    DrawCircle(e);
                    break;
                case HAUM_GRAPHIC_TYPE.Polygon:
                    DrawPolygon(e, true);
                    break;
            }

            mainView.Invalidate();

        }

        /// <summary>
        /// 判断是否能被选中
        /// </summary>
        /// <param name="target">点</param>
        /// <param name="mouse">鼠标点</param>
        /// <param name="relative">鼠标点与中心点距离 容错距离</param>
        /// <returns></returns>
        private bool IsFocus(PointF target, PointF mouse, float relative)
        {
            PointF screen = Tools.ImageTransScreenPointF(target, myGraphic.scale, myGraphic.offset);
            if (Math.Abs(mouse.X - screen.X) < relative && Math.Abs(mouse.Y - screen.Y) < relative)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool FindRecently(PointF[] contours, Point mouse, float relative, out int index)
        {
            index = -1;
            float xyMinDistance = 10240;
            //找到所有距离鼠标点最近的点索引
            for (int i = 0; i < contours.Length; i++)
            {
                PointF screenPoint = Tools.ImageTransScreenPointF(contours[i], myGraphic.scale, myGraphic.offset);
                float dx = Math.Abs(mouse.X - screenPoint.X);
                float dy = Math.Abs(mouse.Y - screenPoint.Y);
                float distance = dx + dy;
                if (distance < xyMinDistance && dx < relative && dy < relative)
                {
                    xyMinDistance = distance;
                    index = i;
                }
            }
            if (index >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 清空绘制图形
        /// </summary>
        public void ClearDraw()
        {
            if (myGraphic.type == HAUM_GRAPHIC_TYPE.None && myGraphic.status == HAUM_GRAPHIC_STATUS.Done)
            {
                haumPoint = null;
                haumLine = null;
                haumRectangle = null;
                haumRotateRect = null;
                haumCircle = null;
                haumPolygon = null;
                haumText = null;
                shapes?.Clear();
            }
            else
            {
                this.CancelDraw();
            }
            mainView.Invalidate();
        }

        /// <summary>
        /// 取消当前绘制
        /// </summary>
        public void CancelDraw()
        {
            switch (myGraphic.type)
            {
                case HAUM_GRAPHIC_TYPE.Point:
                    haumPoint = null;
                    break;
                case HAUM_GRAPHIC_TYPE.Line:
                    haumLine = null;
                    break;
                case HAUM_GRAPHIC_TYPE.Circle:
                    haumCircle = null;
                    break;
                case HAUM_GRAPHIC_TYPE.Ellipse:
                    haumEllipse = null;
                    break;
                case HAUM_GRAPHIC_TYPE.Rectangle:
                    haumRectangle = null;
                    break;
                case HAUM_GRAPHIC_TYPE.RotateRect:
                    haumRotateRect = null;
                    break;
                case HAUM_GRAPHIC_TYPE.Polygon:
                    haumPolygon = null;
                    break;
            }
            myGraphic.type = HAUM_GRAPHIC_TYPE.None;
            myGraphic.status = HAUM_GRAPHIC_STATUS.Done;
            mainView.Cursor = this.cursor;
        }

        /// <summary>
        /// 开始绘制点
        /// </summary>
        public void StartDrawPoint()
        {
            if (hImage != null)
            {
                haumPoint = new HaumPoint(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.Point;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制点过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawPoint(MouseEventArgs e)
        {
            switch (myGraphic.status)
            {
                case HAUM_GRAPHIC_STATUS.Start:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        haumPoint.center = imageLocation;
                        haumPoint.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Finish:
                    //更新点的位置
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        haumPoint.center = imageLocation;
                    }
                    break;
            }

            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {
                haumPoint.isSelect = false;
                graphicPointCallBack?.Invoke(this.haumPoint);
                //结束绘制
                this.DrawEnd();
                this.shapes.Add(haumPoint);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }
        }

        /// <summary>
        /// 开始绘制线
        /// </summary>
        public void StartDrawLine()
        {
            if (hImage != null)
            {
                haumLine = new HaumLine(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.Line;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制线过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawLine(MouseEventArgs e)
        {
            switch (myGraphic.status)
            {

                case HAUM_GRAPHIC_STATUS.Start:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //haumLine.startPoint = Tools.ScreenTransImagePointF(e.Location, myGraphic.scale, myGraphic.offset);
                        //haumLine.endPoint = Tools.ScreenTransImagePointF(e.Location, myGraphic.scale, myGraphic.offset);
                        haumLine.startPoint = imageLocation;
                        haumLine.endPoint = imageLocation;
                        haumLine.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Drawing;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Drawing:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        haumLine.endPoint = imageLocation;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Finish:
                    if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumLine.GetCenterF(), e.Location, 5) ||
                        IsFocus(haumLine.startPoint, e.Location, 5) || IsFocus(haumLine.endPoint, e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeAll;
                        haumLine.isSelect = true;
                    }
                    else
                    {
                        mainView.Cursor = Cursors.AppStarting;
                        haumLine.isSelect = false;
                    }

                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumLine.GetCenterF(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Moving;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumLine.startPoint, e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.StartDraging;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumLine.endPoint, e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.EndDraging;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && !IsFocus(haumLine.GetCenterF(), imageLocation, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Moving:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        float relativeX = imageLocation.X - haumLine.GetCenterF().X;
                        float relativeY = imageLocation.Y - haumLine.GetCenterF().Y;
                        //更新坐标
                        haumLine.startPoint.X += relativeX;
                        haumLine.startPoint.Y += relativeY;
                        haumLine.endPoint.X += relativeX;
                        haumLine.endPoint.Y += relativeY;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.StartDraging:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新起点坐标
                        haumLine.startPoint = imageLocation;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.EndDraging:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新终点坐标
                        haumLine.endPoint = Tools.ScreenTransImagePointF(e.Location, myGraphic.scale, myGraphic.offset);
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
            }

            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {
                haumLine.isSelect = false;
                graphicLineCallBack?.Invoke(this.haumLine);
                this.DrawEnd();
                this.shapes.Add(haumLine);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }
        }

        /// <summary>
        /// 开始绘制圆
        /// </summary>
        public void StartDrawCircle()
        {
            if (hImage != null)
            {
                haumCircle = new HaumCircle(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.Circle;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制圆过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawCircle(MouseEventArgs e)
        {
            switch (myGraphic.status)
            {

                case HAUM_GRAPHIC_STATUS.Start:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        haumCircle.center = imageLocation;
                        haumCircle.radius = 0;
                        haumCircle.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Drawing;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Drawing:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        PointF movePoint = imageLocation;
                        //计算圆半径
                        haumCircle.radius = (float)Math.Sqrt(Math.Pow(Math.Abs(movePoint.X - haumCircle.center.X), 2) + Math.Pow(Math.Abs(movePoint.Y - haumCircle.center.Y), 2));
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Finish:
                    if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumCircle.center, e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeAll;
                        haumCircle.isSelect = true;
                    }
                    else if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumCircle.GetScalePointF(), e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeWE;
                        haumCircle.isSelect = true;
                    }
                    else
                    {
                        mainView.Cursor = Cursors.AppStarting;
                        haumCircle.isSelect = false;
                    }

                    //更新
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumCircle.center, e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Moving;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumCircle.GetScalePointF(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.ScaleDraging;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && !IsFocus(haumCircle.center, e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Moving:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新坐标
                        haumCircle.center = imageLocation;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.ScaleDraging:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新半径
                        haumCircle.radius = imageLocation.X - haumCircle.center.X;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
            }

            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {
                haumCircle.isSelect = false;
                graphicCircleCallBack?.Invoke(this.haumCircle);
                this.DrawEnd();
                this.shapes.Add(haumCircle);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }
        }

        /// <summary>
        /// 开始绘制椭圆
        /// </summary>
        public void StartDrawEllipse()
        {
            if (hImage != null)
            {
                haumEllipse = new HaumEllipse(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.Ellipse;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制椭圆过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawEllipse(MouseEventArgs e)
        {
            switch (myGraphic.status)
            {
                case HAUM_GRAPHIC_STATUS.Start:

                    break;
                case HAUM_GRAPHIC_STATUS.Drawing:

                    break;
                case HAUM_GRAPHIC_STATUS.Finish:

                    break;
                case HAUM_GRAPHIC_STATUS.Moving:

                    break;
            }

            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {

            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }
        }


        /// <summary>
        /// 开始绘制矩形
        /// </summary>
        public void StartDrawRectangle()
        {
            if (hImage != null)
            {
                haumRectangle = new HaumRectangle(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.Rectangle;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制矩形过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawRectangle(MouseEventArgs e)
        {
            switch (myGraphic.status)
            {
                case HAUM_GRAPHIC_STATUS.Start:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        haumRectangle.location = imageLocation;
                        haumRectangle.size = new SizeF(0, 0);
                        haumRectangle.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Drawing;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Drawing:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        PointF rbLoca = imageLocation;
                        haumRectangle.size.Width = Math.Abs(rbLoca.X - haumRectangle.location.X);
                        haumRectangle.size.Height = Math.Abs(rbLoca.Y - haumRectangle.location.Y);
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Finish:
                    //显示
                    if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRectangle.GetCenter(), e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeAll;
                        haumRectangle.isSelect = true;
                    }
                    else if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRectangle.GetBR(), e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeNWSE;
                        haumRectangle.isSelect = true;
                    }
                    else
                    {
                        mainView.Cursor = Cursors.AppStarting;
                        haumRectangle.isSelect = false;
                    }

                    //操作
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRectangle.GetCenter(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Moving;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRectangle.GetBR(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.BrDraging;
                    }
                    else if (e.Button == MouseButtons.Left && !IsFocus(haumRectangle.GetCenter(), e.Location, 5) && !IsFocus(haumRectangle.GetBR(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Moving:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新坐标
                        haumRectangle.location.X = imageLocation.X - haumRectangle.size.Width / 2;
                        haumRectangle.location.Y = imageLocation.Y - haumRectangle.size.Height / 2;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;

                case HAUM_GRAPHIC_STATUS.BrDraging:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新宽高
                        haumRectangle.size.Width = Math.Abs(imageLocation.X - haumRectangle.location.X);
                        haumRectangle.size.Height = Math.Abs(imageLocation.Y - haumRectangle.location.Y);
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
            }

            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {
                haumRectangle.isSelect = false;
                graphicRectangleCallBack?.Invoke(this.haumRectangle);
                this.DrawEnd();
                this.shapes.Add(haumRectangle);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }
        }


        /// <summary>
        /// 开始绘制旋转矩形
        /// </summary>
        public void StartDrawRotateRect()
        {
            if (hImage != null)
            {
                haumRotateRect = new HaumRotateRect(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.RotateRect;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制旋转矩形过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawRotateRect(MouseEventArgs e)
        {
            switch (myGraphic.status)
            {
                case HAUM_GRAPHIC_STATUS.Start:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        haumRotateRect.center = imageLocation;
                        haumRotateRect.widthHalf = 0;
                        haumRotateRect.heightHalf = 0;
                        haumRotateRect.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Drawing;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Drawing:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        float w = imageLocation.X - haumRotateRect.center.X;
                        float h = imageLocation.Y - haumRotateRect.center.Y;
                        haumRotateRect.widthHalf = w;
                        haumRotateRect.heightHalf = h;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Finish:
                    //显示
                    if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRotateRect.center, e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeAll;
                        haumRotateRect.isSelect = true;
                    }
                    else if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRotateRect.GetTR(), e.Location, 10))
                    {
                        mainView.Cursor = Cursors.SizeAll;
                        haumRotateRect.isSelect = true;
                    }
                    else if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRotateRect.GetRightCenter(), e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeWE;
                        haumRotateRect.isSelect = true;
                    }
                    else if (Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRotateRect.GetBottomCenter(), e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeNS;
                        haumRotateRect.isSelect = true;
                    }
                    else
                    {
                        mainView.Cursor = Cursors.AppStarting;
                        haumRotateRect.isSelect = false;
                    }

                    //操作
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRotateRect.center, e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Moving;
                    }
                    else if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size) && IsFocus(haumRotateRect.GetTR(), e.Location, 10))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Rotating;
                    }
                    else if (e.Button == MouseButtons.Left && IsFocus(haumRotateRect.GetRightCenter(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.RightDraging;
                    }
                    else if (e.Button == MouseButtons.Left && IsFocus(haumRotateRect.GetBottomCenter(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.BottomDraging;
                    }
                    else if (e.Button == MouseButtons.Left && !IsFocus(haumRotateRect.center, e.Location, 5) && !IsFocus(haumRotateRect.GetTR(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Moving:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //更新坐标
                        haumRotateRect.center = imageLocation;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Rotating:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        //计算中心与右上角角度
                        double r = Tools.GetRotate(haumRotateRect.center, haumRotateRect.GetTR());
                        //计算鼠标与中心的角度
                        double mr = Tools.GetRotate(haumRotateRect.center, imageLocation);
                        //角点旋转
                        haumRotateRect.angle += mr - r;

                        //角度调节
                        if (Math.Abs(haumRotateRect.angle - 0) < 1)
                        {
                            haumRotateRect.angle = 0;
                        }
                        else if (Math.Abs(haumRotateRect.angle - 90) < 1)
                        {
                            haumRotateRect.angle = 90;
                        }
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.RightDraging:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        float w = imageLocation.X - haumRotateRect.center.X;
                        haumRotateRect.widthHalf = w;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.BottomDraging:
                    if (e.Button == MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        float h = imageLocation.Y - haumRotateRect.center.Y;
                        haumRotateRect.heightHalf = h;
                    }
                    else if (e.Button != MouseButtons.Left && Tools.IsMouseInImage(imageLocation, hImage.size))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
            }
            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {
                haumRotateRect.isSelect = false;
                graphicRotateRectCallBack?.Invoke(this.haumRotateRect);
                this.DrawEnd();
                this.shapes.Add(haumRotateRect);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }
        }

        /// <summary>
        /// 开始绘制多边形
        /// </summary>
        public void StartDrawPolygon()
        {
            if (hImage != null)
            {
                haumPolygon = new HaumPolygon(myGraphic.color, myGraphic.size);
                myGraphic.type = HAUM_GRAPHIC_TYPE.Polygon;
                myGraphic.status = HAUM_GRAPHIC_STATUS.Start;
                mainView.Cursor = Cursors.AppStarting;
            }
        }

        /// <summary>
        /// 绘制多边形过程
        /// </summary>
        /// <param name="e"></param>
        private void DrawPolygon(MouseEventArgs e, bool isClick)
        {
            bool isMouseInImage = Tools.IsMouseInImage(imageLocation, hImage.size);
            if (!isMouseInImage) return;

            switch (myGraphic.status)
            {
                case HAUM_GRAPHIC_STATUS.Start:
                    if ((e.Button == MouseButtons.Left) && isClick)
                    {
                        if (haumPolygon.contours.Count > 1)
                        {
                            haumPolygon.contours = new List<PointF>();
                        }
                        //添加起点
                        haumPolygon.contours.Add(imageLocation);
                        haumPolygon.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Drawing;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Drawing:
                    if (IsFocus(haumPolygon.contours[0], e.Location, 5))
                    {
                        mainView.Cursor = Cursors.Hand;
                    }
                    else
                    {
                        mainView.Cursor = Cursors.AppStarting;
                    }

                    if (e.Button == MouseButtons.Left && isClick)
                    {
                        //添加路径

                        if (IsFocus(haumPolygon.contours[0], e.Location, 5))
                        {
                            PointF start = haumPolygon.contours[0];
                            haumPolygon.contours.Add(start);
                            haumPolygon.isConnectd = true;
                            myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                        }
                        else
                        {
                            haumPolygon.contours.Add(imageLocation);
                        }
                    }
                    else if (e.Button == MouseButtons.Left && IsFocus(haumPolygon.GetCenter(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Moving;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Finish:
                    if (IsFocus(haumPolygon.GetCenter(), e.Location, 5))
                    {
                        mainView.Cursor = Cursors.SizeAll;
                        haumPolygon.isSelect = true;
                    }
                    else
                    {
                        mainView.Cursor = Cursors.AppStarting;
                        haumPolygon.isSelect = false;
                    }

                    bool isFindRecently = FindRecently(haumPolygon.contours.ToArray(), e.Location, 5, out int index);
                    if (isFindRecently)
                    {
                        haumPolygon.contourIndex = index;
                        haumPolygon.isDroging = true;
                    }
                    else
                    {
                        haumPolygon.isDroging = false;
                    }

                    if (e.Button == MouseButtons.Left && IsFocus(haumPolygon.GetCenter(), e.Location, 5))
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Moving;
                    }
                    else if (e.Button == MouseButtons.Left && isFindRecently)
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.PointOffset;
                    }
                    else if (e.Button == MouseButtons.Left && isClick)
                    {
                        if (haumPolygon.contours.Count > 1)
                        {
                            haumPolygon.contours = new List<PointF>();
                        }
                        //添加起点
                        haumPolygon.contours.Add(imageLocation);
                        haumPolygon.startGraphic = true;
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Drawing;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.PointOffset:
                    if (e.Button == MouseButtons.Left)
                    {
                        //更新坐标
                        PointF newPosition = imageLocation;
                        PointF position = haumPolygon.contours[haumPolygon.contourIndex];
                        PointF pathOffset = new PointF(newPosition.X - position.X, newPosition.Y - position.Y);
                        haumPolygon.PathOffset(pathOffset);
                    }
                    else if (e.Button != MouseButtons.Left && haumPolygon.isConnectd)
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
                case HAUM_GRAPHIC_STATUS.Moving:
                    if (e.Button == MouseButtons.Left)
                    {
                        //更新坐标
                        PointF newCenter = imageLocation;
                        haumPolygon.ContoursOffset(newCenter.X - haumPolygon.GetCenter().X, newCenter.Y - haumPolygon.GetCenter().Y);
                    }
                    else if (e.Button != MouseButtons.Left)
                    {
                        myGraphic.status = HAUM_GRAPHIC_STATUS.Finish;
                    }
                    break;
            }

            if (e.Button == MouseButtons.Right && myGraphic.status == HAUM_GRAPHIC_STATUS.Finish)
            {
                haumPolygon.isSelect = false;
                graphicPolygonCallBack?.Invoke(this.haumPolygon);
                this.DrawEnd();
                this.shapes.Add(haumPolygon);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.CancelDraw();
            }

        }

        public void WriteText(string message, Point location)
        {
            haumText = new HaumText(message, 2, this.myGraphic.color, myGraphic.font, location);
            mainView.Invalidate();
        }

        private void ShowText(PaintEventArgs e)
        {
            e.Graphics.DrawString(haumText.text, haumText.font, haumText.brush, haumText.location);
        }

        /// <summary>
        /// 结束绘制
        /// </summary>
        public void DrawEnd()
        {
            myGraphic.type = HAUM_GRAPHIC_TYPE.None;
            myGraphic.status = HAUM_GRAPHIC_STATUS.Done;
            mainView.Cursor = this.cursor;
        }

        /// <summary>
        /// 图像X水平翻转
        /// </summary>
        public void FlipX()
        {
            if (this.isDisplaying)
            {
                hImage.bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
                if (myGraphic.showThums)
                {
                    thumbnailDic[0].RotateFlip(RotateFlipType.RotateNoneFlipX);
                    thumbnailDic[1].RotateFlip(RotateFlipType.RotateNoneFlipX);
                    thumbnailDic[2].RotateFlip(RotateFlipType.RotateNoneFlipX);
                }
                this.Fit();
            }
        }

        /// <summary>
        /// 图形Y垂直翻转
        /// </summary>
        public void FlipY()
        {
            if (this.isDisplaying)
            {
                hImage.bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
                if (myGraphic.showThums)
                {
                    thumbnailDic[0].RotateFlip(RotateFlipType.RotateNoneFlipY);
                    thumbnailDic[1].RotateFlip(RotateFlipType.RotateNoneFlipY);
                    thumbnailDic[2].RotateFlip(RotateFlipType.RotateNoneFlipY);
                }
                this.Fit();
            }
        }

        /// <summary>
        /// 图像顺时针旋转
        /// </summary>
        public void LeftRotate()
        {
            if (this.isDisplaying)
            {
                hImage.bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
                if (myGraphic.showThums)
                {
                    thumbnailDic[0].RotateFlip(RotateFlipType.Rotate270FlipNone);
                    thumbnailDic[1].RotateFlip(RotateFlipType.Rotate270FlipNone);
                    thumbnailDic[2].RotateFlip(RotateFlipType.Rotate270FlipNone);
                }
                this.Fit();
            }
        }

        /// <summary>
        /// 图形逆时针旋转
        /// </summary>
        public void RightRotate()
        {
            if (this.isDisplaying)
            {
                hImage.bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
                if (myGraphic.showThums)
                {
                    thumbnailDic[0].RotateFlip(RotateFlipType.Rotate90FlipNone);
                    thumbnailDic[1].RotateFlip(RotateFlipType.Rotate90FlipNone);
                    thumbnailDic[2].RotateFlip(RotateFlipType.Rotate90FlipNone);
                }
                this.Fit();
            }
        }

        /// <summary>
        /// 旋转后恢复图像
        /// </summary>
        public void Restore()
        {
            this.Fit();
        }

        /// <summary>
        /// 菜单栏清空图形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_clear_Click(object sender, EventArgs e)
        {
            this.ClearDraw();
        }

        /// <summary>
        /// 鼠标样式切换到箭头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void toolStripButton_arrow_Click(object sender, EventArgs e)
        {
            if (myGraphic.type == HAUM_GRAPHIC_TYPE.None)
            {
                this.cursor = Cursors.Arrow;
                mainView.Cursor = Cursors.Arrow;
                toolStripButton_arrow.Checked = true;
                toolStripButton_hand.Checked = false;
            }
        }

        /// <summary>
        /// 鼠标样式切换到抓手
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void toolStripButton_hand_Click(object sender, EventArgs e)
        {
            if (myGraphic.type == HAUM_GRAPHIC_TYPE.None)
            {
                this.cursor = Cursors.Hand;
                mainView.Cursor = Cursors.Hand;
                toolStripButton_arrow.Checked = false;
                toolStripButton_hand.Checked = true;
            }
        }

        /// <summary>
        /// 自适应显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_fit_Click(object sender, EventArgs e)
        {
            this.Fit();
        }

        /// <summary>
        /// 缩放选项点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripSplitButton_scale_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (hImage == null) { return; }

            float oldScale = myGraphic.scale;

            switch (e.ClickedItem.Text)
            {
                case "50%":
                    myGraphic.scale = 0.5f;
                    break;
                case "100%":
                    myGraphic.scale = 1f;
                    break;
                case "150%":
                    myGraphic.scale = 1.5f;
                    break;
                case "200%":
                    myGraphic.scale = 2f;
                    break;
            }

            float dx = mainView.Width / 2 - myGraphic.offset.X;
            float dy = mainView.Height / 2 - myGraphic.offset.Y;
            myGraphic.offset.X -= dx * (myGraphic.scale / oldScale - 1);
            myGraphic.offset.Y -= dy * (myGraphic.scale / oldScale - 1);
            myGraphic.scaleSize.Width = (int)(hImage.bmp.Width * myGraphic.scale);
            myGraphic.scaleSize.Height = (int)(hImage.bmp.Height * myGraphic.scale);
            toolStripSplitButton_scale.Text = $"{(myGraphic.scale * 100).ToString("0")}%";
            mainView.Invalidate();
        }

        /// <summary>
        /// 菜单栏顺时针旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_leftRotate_Click(object sender, EventArgs e)
        {
            LeftRotate();
        }
        /// <summary>
        /// 菜单栏逆时针旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_rightRotate_Click(object sender, EventArgs e)
        {
            RightRotate();
        }


        private void toolStripMenuItem_point_Click(object sender, EventArgs e)
        {
            StartDrawPoint();
        }

        private void toolStripMenuItem_line_Click(object sender, EventArgs e)
        {
            StartDrawLine();
        }

        private void toolStripMenuItem_rectangle_Click(object sender, EventArgs e)
        {
            StartDrawRectangle();
        }

        private void toolStripMenuItem_circle_Click(object sender, EventArgs e)
        {
            StartDrawCircle();
        }

        /// <summary>
        /// 绘制区域控件鼠标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mainView_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void toolStripMenuItem_polygon_Click(object sender, EventArgs e)
        {
            StartDrawPolygon();
        }

        private void toolStripMenuItem_rotateRect_Click(object sender, EventArgs e)
        {
            StartDrawRotateRect();
        }

        /// <summary>
        /// 获取绘制的点
        /// </summary>
        /// <returns></returns>
        public HaumPoint GetDrawPoint()
        {
            return haumPoint;
        }
        /// <summary>
        /// 获取绘制的线
        /// </summary>
        /// <returns></returns>
        public HaumLine GetDrawLine()
        {
            return haumLine;
        }
        /// <summary>
        /// 获取绘制的圆
        /// </summary>
        /// <returns></returns>
        public HaumCircle GetDrawCircle()
        {
            return haumCircle;
        }
        /// <summary>
        /// 获取绘制的矩形
        /// </summary>
        /// <returns></returns>
        public HaumRectangle GetDrawRectangle()
        {
            return haumRectangle;
        }
        /// <summary>
        /// 获取绘制的旋转矩形
        /// </summary>
        /// <returns></returns>
        public HaumRotateRect GetDrawRotateRect()
        {
            return haumRotateRect;
        }
        /// <summary>
        /// 获取绘制的多边形
        /// </summary>
        /// <returns></returns>
        public HaumPolygon GetDrawPolygon()
        {
            return haumPolygon;
        }

        /// <summary>
        /// 获取一个指定类型的绘制图形
        /// </summary>
        /// <param name="type">图形类型</param>
        /// <returns>指定绘制类型的图形对象</returns>
        public Object GetRequireGraphic(Type type)
        {
            if (type == typeof(HaumPoint))
            {
                return haumPoint;
            }
            else if (type == typeof(HaumLine))
            {
                return haumLine;
            }
            else if (type == typeof(HaumCircle))
            {
                return haumCircle;
            }
            else if (type == typeof(HaumEllipse))
            {
                return haumEllipse;
            }
            else if (type == typeof(HaumRectangle))
            {
                return haumRectangle;
            }
            else if (type == typeof(HaumRotateRect))
            {
                return haumRotateRect;
            }
            else if (type == typeof(HaumPolygon))
            {
                return haumPolygon;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HaumDisplayWindow_Load(object sender, EventArgs e)
        {
            listenKeys = new Thread(ListenKeys);
            listenKeys.Start();
        }

        private void mainView_MouseLeave(object sender, EventArgs e)
        {
            isMouseInMainView = false;
        }

        private void mainView_MouseEnter(object sender, EventArgs e)
        {
            isMouseInMainView = true;
        }

        private void HaumDisplayWindow_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                // 获取拖放的文件
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                if (files.Length > 0)
                {
                    // 获取第一个文件路径
                    string filePath = files[0];

                    // 检查文件是否为图片
                    if (IsImageFile(filePath))
                    {
                        // 显示图片
                        this.LoadImage(filePath, true);
                    }
                    else
                    {
                        MessageBox.Show("请拖放有效的图片文件！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("处理图片时出错: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void HaumDisplayWindow_DragEnter(object sender, DragEventArgs e)
        {
            // 检查拖放的数据是否包含文件
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // 获取拖放的文件
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                // 检查第一个文件是否为图片文件
                if (files.Length == 1 && IsImageFile(files[0]))
                {
                    // 允许复制操作
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    // 不允许拖放
                    e.Effect = DragDropEffects.None;
                }
            }
            else
            {
                // 不允许拖放
                e.Effect = DragDropEffects.None;
            }
        }

        private bool IsImageFile(string filePath)
        {
            // 获取文件扩展名并转换为小写
            string extension = Path.GetExtension(filePath).ToLower();

            // 检查文件扩展名是否为常见的图片格式
            switch (extension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                case ".png":
                case ".bmp":
                case ".gif":
                case ".tif":
                case ".tiff":
                    return true;
                default:
                    return false;
            }
        }
    }
}
